package cn.gov.pbc.gd.credit.web.controller.report;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.liu.jbase.PageListVo;
import com.liu.jbase.ResultVo;
import com.liu.jbase.constant.ResCode;
import com.liu.jbase.controller.impl.BaseControllerImpl;

import cn.gov.pbc.gd.credit.core.service.report.IReportQueryService;
import cn.gov.pbc.gd.credit.vo.report.query.ReportQueryVo;
import cn.gov.pbc.gd.credit.vo.report.query.ResQueryDetailVo;
import cn.gov.pbc.gd.credit.vo.report.query.ResQueryTotalVo;

@Controller
@RequestMapping("/report/query")
public class ReportQueryController extends BaseControllerImpl<ResQueryTotalVo, ReportQueryVo, IReportQueryService>{

	@Autowired
	private IReportQueryService reportQueryService;
	
	@RequestMapping("/total")
	public String queryTotal(ReportQueryVo vo, Model model, HttpServletRequest request){
		
		String cid = vo.getCorpId();
		
		ResultVo<List<ResQueryTotalVo>> resultVo = reportQueryService.queryTotal(vo, request);
		model.addAttribute("result", resultVo);
		vo.setCorpId(cid);
		model.addAttribute("queryVo", JSON.toJSON(vo));
		
	    return "report/queryTotal";	
	}
	
	@RequestMapping(value="/excel", method = RequestMethod.GET)
	public void downloadFile(ReportQueryVo vo, HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		ResultVo<String> resultVo = reportQueryService.exportQueryTotal(vo, request, response);
		
		if( ! ResCode.SUCCESS.equals(resultVo.getCode())){
			return ;
		}

	}
	
	@RequestMapping("/page/list/view")
	public String pageList(ReportQueryVo vo, Model model, HttpServletRequest request){
		
		String cid = vo.getCorpId();
		
		ResultVo<PageListVo<ResQueryDetailVo>> resultVo = reportQueryService.pageList(vo, request);
		model.addAttribute("result", resultVo);
		vo.setCorpId(cid);
		model.addAttribute("queryVo", JSON.toJSON(vo));
		
	    return "report/pageList";	
	}
	
	@RequestMapping("/page/again/print")
	public String againPrintPage(ReportQueryVo vo, Model model, HttpServletRequest request){
		
		String cid = vo.getCorpId();
		vo.setPrintResult(3);
		ResultVo<PageListVo<ResQueryDetailVo>> resultVo = reportQueryService.pageList(vo, request);
		model.addAttribute("result", resultVo);
		vo.setCorpId(cid);
		model.addAttribute("queryVo", JSON.toJSON(vo));
		
	    return "report/pageAgainPrint";	
	}
	
	@RequestMapping(value="/export/list", method = RequestMethod.GET)
	public void exportList(ReportQueryVo vo, HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		ResultVo<List<ResQueryDetailVo>> resultVo = reportQueryService.exportList(vo, request, response);
		
		if( ! ResCode.SUCCESS.equals(resultVo.getCode())){
			return ;
		}

	}
}
